package com.ware.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XlsFileUtil {

	public static List<HashMap<String, Object>> parseData(File file, String[] srcHeader, String[] ObjHeader) throws Exception{
		return parseData(file, srcHeader, ObjHeader, 0);
	}
	
	public static List<HashMap<String, Object>> parseData(File file, String[] srcHeader, String[] ObjHeader, int type) throws Exception{
		if (srcHeader.length != ObjHeader.length) {
			throw new Exception("目标字段与源字段不一致。");
		}
		
		Workbook workbook;
		try {
			InputStream input = new FileInputStream(file);
			
			if (file.getName().endsWith("xlsx")) {
				workbook = new XSSFWorkbook(input);  
			}else {
				workbook = new HSSFWorkbook(input);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("解析文件失败，请稍后再试。");
		}
		
		Sheet sheet = workbook.getSheetAt(0);
		
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		
		List<String> headers = new ArrayList<String>();
		
		Row firstRow = sheet.getRow(0);
		for (int col = 0; col < firstRow.getLastCellNum(); col++) {
			headers.add(firstRow.getCell(col).getStringCellValue());
		}
		
		for (String s : srcHeader) {
			if (!headers.contains(s)) {
				throw new Exception("未找到目标字段【" + s + "】");
			}
		} 
		
		for (int row = 1; row <= sheet.getLastRowNum(); row++) {
			
			HashMap<String, Object> rowData = new HashMap<String, Object>();
			
			Row r = sheet.getRow(row);
			for (int col = 0; col < r.getLastCellNum(); col++) {
				
				Cell cell = r.getCell(col);
				
				if (cell != null) {
					switch (cell.getCellType()) {
					
					case HSSFCell.CELL_TYPE_STRING :
						rowData.put(ObjHeader[col], cell.getStringCellValue());
						break;
						
					case HSSFCell.CELL_TYPE_NUMERIC :
						if (HSSFDateUtil.isCellDateFormatted(cell)) {
							Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
							if (type == 1) {
								rowData.put(ObjHeader[col], new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));
							}else {
								rowData.put(ObjHeader[col], new SimpleDateFormat("yyyy-MM-dd").format(date));
							}
						}else {
							cell.setCellType(HSSFCell.CELL_TYPE_STRING);
							rowData.put(ObjHeader[col], cell.getStringCellValue());
						}
						break;
						
					default: rowData.put(ObjHeader[col], cell.getStringCellValue());
					}
				}
			}
			
			data.add(rowData);
		}
		
		return data;
	}
}
